import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from '@/pages/Index.vue'
import Home from '@/pages/router-test/Home.vue'
import About from '@/pages/router-test/About.vue'
import News from '@/pages/router-test/News.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Index',
    component: Index,
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
  },
  {
    path: '/about',
    name: 'About',
    component: About,
  },
  {
    path: '/news',
    name: 'News',
    component: News,
  },
]

const router = new VueRouter({
  mode: 'history',
  routes,
})

// 全局导航守卫
router.beforeEach((to, from, next) => {
  if (to.name === 'About') {
    next('/news')
  } else {
    next()
  }
})

export function createRouter() {
  return router
}
